<?php
namespace app\workers\controller;

use app\model\WorkersUserModel;
use think\Validate;

class Login
{
    protected $mode;

    public function __construct()
    {
        $this->mode = new WorkersUserModel();
    }

    /**
     * 账号登录
     * @return false|string
     */
    public function index(): bool|string
    {
        $param = input();
        try {
            $validate = new Validate();
            if (!$validate->check($param, [
                'id_card|身份证号' => 'require',
                'name|姓名' => 'require',
            ]))
                throw new \Exception($validate->getError());
            $user = $this->mode->where('id_card', '=', $param['id_card'])->find();
            if (!$user) throw new \Exception('身份错误！');
            if ($user['name'] != $param['name']) throw new \Exception('身份错误！');
            $user['token'] = generateLoginToken($user->toArray(),'workers_');
            return success($user);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}